package linear;

public class ReversOnlyLetters917 {
    public static String reverseOnlyLetters(String S) {
        StringBuilder builder = new StringBuilder();
        int i = 0;
        int j = S.length() - 1;
        while (i < S.length()) {
            if (Character.isLetter(S.charAt(i)) && Character.isLetter(S.charAt(j))) {
                builder.append(S.charAt(j));
                i++;
                j--;
            } else if (!Character.isLetter(S.charAt(i))) {
                builder.append(S.charAt(i));
                i++;
            } else if (!Character.isLetter(S.charAt(j))) {
                j--;
            }
        }

        return builder.toString();
    }

    public static void main(String[] args) {
        System.out.println(reverseOnlyLetters("b-acd---"));
    }
}
